import QtQuick
import QtQuick.Controls

Window {
    id:nsnWindow
    x:root.x-320
    y:root.y-160
    width: 320
    height: 240
    visible: true
    title: "新建便利贴"
    flags: Qt.FramelessWindowHint
    Rectangle{
        width:parent.width
        height:parent.height
        //var now = new Date()
        TextArea {
            id:contentSN
            width:parent.width
            height:nameSwitch.y
            wrapMode: Text.Wrap
            placeholderText: "新建便利贴:请输入多行文本..."
        }
        Text {
            id: nameSwitch
            x:15
            y:parent.height-47
            font.pixelSize:12
            text: qsTr("提醒")
        }
        Switch {
            id: switchWarn
            y:parent.height-32
        }
        ComboBox {
            id: comboBoxDate
            x: switchWarn.width+10
            y: parent.height-32
            enabled: switchWarn.checked // 绑定到 Switch 的状态
            width:60
            model:weekdays
        }
        ComboBox {
            id: comboBoxTime
            x: switchWarn.width+80
            y: parent.height-32
            enabled: switchWarn.checked // 绑定到 Switch 的状态
            width:60
            model:["00:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00","04:30",
               "05:00","05:30","06:00","06:30","07:00","07:30","08:00","08:30","09:00","09:30",
               "10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00",
               "14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30",
               "19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:00","23:30"]
        }
        Button {
            text: "取消"
            x:parent.width-80
            y:parent.height-34
            width:35
            onClicked: {
                nsnWindow.destroy()
            }
        }
        Button {
            text: "提交"
            x:parent.width-40
            y:parent.height-34
            width:35
            onClicked: {
                if(contentSN.text.length>2){
                    var dateTimeString ="2025-"+comboBoxDate.currentText+"T"+comboBoxTime.currentText+":00"
                    var t1=new Date().getTime()
                    var t2=new Date(dateTimeString).getTime()
                    if(!switchWarn.checked)t2=0
                    if(localDB.writeStickyNote(root.userId,"    "+contentSN.text,t1,t2)){
                        if(localConfig.readLocalConfig("warnchecked")){
                            warnR.model=localDB.readWarnTimeSN(root.userId)
                        }
                        nsnNoticeOKText.text="便签已经保存！"
                        localConfig.writeLocalConfig("stickynote",(parseInt(localConfig.readLocalConfig("stickynote")) + 1).toString())
                        nsnNoticeOK.open()
                    }
                }
            }
        }
    }
    Dialog{
        id:nsnNoticeOK//通知
        width:160
        height:100
        anchors.centerIn: parent
        title: qsTr("通知!")
        Text {
            id: nsnNoticeOKText
        }
        standardButtons: Dialog.Ok
        onAccepted:nsnWindow.destroy()
    }

    // 获取今天的日期
    property var today: new Date()

    // 计算今天、明天、后天的星期几
    function getWeekdayNames(date) {
        //print(date.getTime());
        //print(new Date(date.getTime()));
        return Qt.formatDateTime(new Date(date.getTime()), "MM-dd");
    }

    property var weekdays: [
        getWeekdayNames(today), // 今天
        getWeekdayNames(new Date(today.getTime() + 24 * 60 * 60 * 1000)), // 明天
        getWeekdayNames(new Date(today.getTime() + 2 * 24 * 60 * 60 * 1000)), // 后天
        getWeekdayNames(new Date(today.getTime() + 3 * 24 * 60 * 60 * 1000)), // 后天
        getWeekdayNames(new Date(today.getTime() + 4 * 24 * 60 * 60 * 1000)), // 后天
        getWeekdayNames(new Date(today.getTime() + 5 * 24 * 60 * 60 * 1000)), // 后天
        getWeekdayNames(new Date(today.getTime() + 6 * 24 * 60 * 60 * 1000)) // 后天
    ]

}
